package com.flnet.web.controller.foxwash;

import com.flnet.common.annotation.Log;
import com.flnet.common.base.AjaxResult;
import com.flnet.common.enums.BusinessType;
import com.flnet.common.page.TableDataInfo;
import com.flnet.common.utils.UUIDUtils;
import com.flnet.common.utils.poi.ExcelUtil;
import com.flnet.foxwash.domain.*;
import com.flnet.foxwash.service.IWMachineService;
import com.flnet.foxwash.service.IWServiceTypeService;
import com.flnet.foxwash.service.IWShopService;
import com.flnet.foxwash.service.IWVendorService;
import com.flnet.framework.util.ShiroUtils;
import com.flnet.framework.web.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理 信息操作处理
 * 
 * @author flnet
 * @date 2019-03-20
 */
@Controller
@RequestMapping("/foxwash/wMachine")
public class WMachineController extends BaseController
{
    private String prefix = "foxwash/wMachine";
	
	@Autowired
	private IWMachineService wMachineService;

	@Autowired
	private IWShopService wShopService;

	@Autowired
	private IWServiceTypeService wServiceTypeService;

	@Autowired
	private IWVendorService wVendorService;
	
	@RequiresPermissions("foxwash:wMachine:view")
	@GetMapping()
	public String wMachine(ModelMap mmap)
	{
		mmap.put("shopList",wShopService.selectWShopList(null));
		mmap.put("serviceTypeList",wServiceTypeService.selectWServiceTypeList(null));
		mmap.put("vendorList",wVendorService.selectWVendorList(null));
	    return prefix + "/wMachine";
	}
	
	/**
	 * 查询设备管理列表
	 */
	@RequiresPermissions("foxwash:wMachine:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WMachine wMachine)
	{
		startPage();
        List<WMachine> list = wMachineService.selectWMachineList(wMachine);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出设备管理列表
	 */
	@RequiresPermissions("foxwash:wMachine:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WMachine wMachine)
    {
    	List<WMachine> list = wMachineService.selectWMachineList(wMachine);
        ExcelUtil<WMachine> util = new ExcelUtil<WMachine>(WMachine.class);
        return util.exportExcel(list, "wMachine");
    }
	
	/**
	 * 新增设备管理
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.addAttribute("typeList",wServiceTypeService.selectWServiceTypeList(null));
		mmap.addAttribute("vendorList",wVendorService.selectWVendorList(null));
		mmap.addAttribute("shopList",wShopService.selectWShopList(null));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存设备管理
	 */
	@RequiresPermissions("foxwash:wMachine:add")
	@Log(title = "设备管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WMachine wMachine)
	{
		wMachine.setUuid(UUIDUtils.getUUID());
		wMachine.setCreator(ShiroUtils.getUserId());
		return toAjax(wMachineService.insertWMachine(wMachine));
	}

	/**
	 * 修改设备管理
	 */
	@GetMapping("/edit/{machineId}")
	public String edit(@PathVariable("machineId") Long machineId, ModelMap mmap)
	{
		WMachine wMachine = wMachineService.selectWMachineById(machineId);
		mmap.put("wMachine", wMachine);

		WServiceType serviceDO = wServiceTypeService.selectWServiceTypeById(wMachine.getTypeId());
		List<WServiceType> serviceList = wServiceTypeService.selectWServiceTypeList(null);
		WServiceType s = new WServiceType();
		if(serviceDO!=null && serviceDO.getTypeId()!=null && serviceList!=null){
			for(WServiceType service:serviceList){
				if(service.getTypeId()==serviceDO.getTypeId()){
					s = service;
				}
			}
			serviceList.remove(s);
		}
		mmap.addAttribute("typeList",serviceList);
		mmap.addAttribute("type",serviceDO);

		WVendor vendorDO = wVendorService.selectWVendorById(wMachine.getVendorId());
		List<WVendor> vendorList = null;
		vendorList = wVendorService.selectWVendorList(null);
		WVendor s2 = new WVendor();
		if(vendorDO!=null && vendorDO.getVendorId()!=null && vendorList!=null){
			for(WVendor wvendor:vendorList){
				if(wvendor.getVendorId()==vendorDO.getVendorId()){
					s2 = wvendor;
				}
			}
			vendorList.remove(s2);
		}
		mmap.addAttribute("vendorList",vendorList);
		mmap.addAttribute("vendor",vendorDO);

		WShop shopDO = wShopService.selectWShopById(wMachine.getShopId());
		List<WShop> shopList = null;
		shopList = wShopService.selectWShopList(null);
		WShop s3 = new WShop();
		if(shopDO!=null && shopDO.getShopId()!=null && shopList!=null){
			for(WShop wshop:shopList){
				if(wshop.getShopId()==shopDO.getShopId()){
					s3 = wshop;
				}
			}
			shopList.remove(s3);
		}
		mmap.addAttribute("shopList",shopList);
		mmap.addAttribute("shop",shopDO);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存设备管理
	 */
	@RequiresPermissions("foxwash:wMachine:edit")
	@Log(title = "设备管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WMachine wMachine)
	{
		wMachine.setModifier(ShiroUtils.getUserId());
		return toAjax(wMachineService.updateWMachine(wMachine));
	}
	
	/**
	 * 删除设备管理
	 */
	@RequiresPermissions("foxwash:wMachine:remove")
	@Log(title = "设备管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wMachineService.deleteWMachineByIds(ids));
	}

	/**
	 * 状态修改
	 */
	@Log(title = "机器管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("foxwash:wMachine:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(WMachine wMachine)
	{
		return toAjax(wMachineService.changeStatus(wMachine));
	}
}
